package jvlite.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import jvlite.core.JLiteContext;
import jvlite.util.StrUtil;
import myJlite.entity.StaffVO;

public class JliteFilter implements Filter {

	private ServletContext context;

	/**
	 * Default constructor.
	 */
	public JliteFilter() {
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see Filter#destroy()
	 */
	public void destroy() {
		// FilterRequestContext.get().end();
	}

	/**
	 * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain)
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest req = (HttpServletRequest) request;
		HttpServletResponse res = (HttpServletResponse) response;
		FilterRequestContext.begin(this.context, req, res);
		Object obj = FilterRequestContext.get().getRequest().getSession().getAttribute("curUser");
		if (StrUtil.objNotEmpty(obj) && obj instanceof StaffVO) {
			JLiteContext.curUser = ((StaffVO)obj);
		}
//		System.out.println("request:"+ FilterRequestContext.get().getRequest().getParameter("id"));
		chain.doFilter(request, response);
	}

	/**
	 * @see Filter#init(FilterConfig)
	 */
	public void init(FilterConfig fConfig) throws ServletException {
	}

}